package xyz.zdsoft.course_manage_module.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import xyz.zdsoft.common.utils.JwtUtils;
import xyz.zdsoft.common.utils.ResultBody;
import xyz.zdsoft.course_manage_module.bo.CourseInfoBo;
import xyz.zdsoft.course_manage_module.bo.CourseStudentInfoBo;
import xyz.zdsoft.course_manage_module.mapper.TeacherCourseMapper;
import xyz.zdsoft.course_manage_module.service.TeacherCourseService;
import xyz.zdsoft.course_manage_module.utils.NotNull;
import xyz.zdsoft.course_manage_module.vo.CourseInfoVo;
import xyz.zdsoft.course_manage_module.vo.CourseStudentInfoVo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-07-25 周日
 * @time 21:21
 * @description
 */
@Service
public class TeacherCourseServiceImpl implements TeacherCourseService {

    @Resource
    private TeacherCourseMapper teacherCourseMapper;

    /**
     * @Description: 查看所有的课程安排
     * @Param: [courseInfoBo]
     * @return: xyz.zdsoft.common.utils.ResultBody
     * @Author: 王林
     * @Date: 2021/7/25
     */
    @Override
    public ResultBody queryAllCourse(CourseInfoBo courseInfoBo, HttpServletRequest request) {

//        通过请求头获取教师的id
        Integer teacherId = Integer.parseInt(JwtUtils.getMemberIdByJwtToken(request));
        courseInfoBo.setTeacherId(teacherId);
//        设置学生的id为空
        courseInfoBo.setStudentId(null);

        Page<CourseInfoVo> page = new Page<>(courseInfoBo.getPageNumber(), courseInfoBo.getPageSize());

        QueryWrapper<CourseInfoVo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(NotNull.judge(courseInfoBo.getTeacherId()), "teacher_id", courseInfoBo.getTeacherId());
        queryWrapper.like(NotNull.judge(courseInfoBo.getCourseName()), "course_name", courseInfoBo.getCourseName());
        queryWrapper.like(NotNull.judge(courseInfoBo.getTrainTeacher()), "train_teacher", courseInfoBo.getTrainTeacher());
        queryWrapper.like(NotNull.judge(courseInfoBo.getDutyTeacher()), "duty_teacher", courseInfoBo.getDutyTeacher());
        queryWrapper.ge(NotNull.judge(courseInfoBo.getStartDate()), "project_date", courseInfoBo.getStartDate());
        queryWrapper.le(NotNull.judge(courseInfoBo.getEndDate()), "project_date", courseInfoBo.getEndDate());

        return ResultBody.success(teacherCourseMapper.queryAllCourse(page, queryWrapper));

    }

    /**
     * @Description: 查询课程里面的学生信息
     * @Param: [courseId, pageNumber, pageSize]
     * @return: xyz.zdsoft.common.utils.ResultBody
     * @Author: 王林
     * @Date: 2021/7/25
     */
    @Override
    public ResultBody queryCourseStudent(CourseStudentInfoBo courseStudentInfoBo) {

        Page<CourseStudentInfoVo> page = new Page<>(courseStudentInfoBo.getPageNumber(), courseStudentInfoBo.getPageSize());

        QueryWrapper<CourseStudentInfoVo> queryWrapper = new QueryWrapper<>();
        Integer courseId = courseStudentInfoBo.getCourseId();

        queryWrapper.eq(NotNull.judge(courseStudentInfoBo.getTeamName()), "team_name", courseStudentInfoBo.getTeamName());
        queryWrapper.eq(NotNull.judge(courseStudentInfoBo.getMajorName()), "major_name", courseStudentInfoBo.getMajorName());
        queryWrapper.eq(NotNull.judge(courseStudentInfoBo.getGradeName()), "grade_name", courseStudentInfoBo.getGradeName());
        queryWrapper.eq(NotNull.judge(courseStudentInfoBo.getClassName()), "class_name", courseStudentInfoBo.getClassName());
        queryWrapper.like(NotNull.judge(courseStudentInfoBo.getStudentName()), "student_name", courseStudentInfoBo.getStudentName());
        queryWrapper.eq(NotNull.judge(courseStudentInfoBo.getUserNo()), "user_no", courseStudentInfoBo.getUserNo());

        return ResultBody.success(teacherCourseMapper.queryCourseStudentInfo(page, queryWrapper, courseId));
    }
}
